package com.sanshi.project.assets.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.BuildingDTO;
import com.sanshi.project.admin.api.entity.BuildingEntity;
import com.sanshi.project.admin.api.entity.CodeRuleEntity;
import com.sanshi.project.assets.service.BuildingService;
import com.sanshi.project.assets.service.CodeRuleService;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 楼栋
 *
 * @author nikon
 * @date 2023-11-17 17:02:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/building")
@Tag(description = "building", name = "楼栋管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BuildingController {

    private final BuildingService buildingService;
    private final CodeRuleService codeRuleService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param building 楼栋
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('assets_building_view')")
    public R getBuildingPage(@ParameterObject Page page, @ParameterObject BuildingDTO building) {
        LambdaQueryWrapper<BuildingEntity> wrapper = Wrappers.lambdaQuery();
        // 项目
        wrapper.in(ArrayUtil.isNotEmpty(building.getProjectId()), BuildingEntity::getProjectId, building.getProjectId());
        // 楼栋编号
        wrapper.like(StrUtil.isNotBlank(building.getCode()), BuildingEntity::getCode, building.getCode());
        // 楼栋名称
        wrapper.like(StrUtil.isNotBlank(building.getName()), BuildingEntity::getName, building.getName());
        // 建筑面积
        if (ArrayUtil.isNotEmpty(building.getFloorArea()))
            wrapper.ge(BuildingEntity::getFloorArea, building.getFloorArea()[0]).le(BuildingEntity::getFloorArea, building.getFloorArea()[1]);
        // 使用面积
        if (ArrayUtil.isNotEmpty(building.getUseArea()))
            wrapper.ge(BuildingEntity::getUseArea, building.getUseArea()[0]).le(BuildingEntity::getUseArea, building.getUseArea()[1]);
        return R.ok(buildingService.page(page, wrapper));
    }


    /**
     * 通过id查询楼栋
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('assets_building_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(buildingService.getById(id));
    }

    /**
     * 新增楼栋
     *
     * @param building 楼栋
     * @return R
     */
    @Operation(summary = "新增楼栋", description = "新增楼栋")
    @SysLog("新增楼栋")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_building_add')")
    public R save(@RequestBody BuildingEntity building) {
        CodeRuleEntity codeRuleEntity = new CodeRuleEntity();
        codeRuleEntity.setType("20");
        R<String> resultCode = codeRuleService.generateCode(codeRuleEntity);
        if (resultCode.isOk()) {
            // 赋予楼栋编码
            building.setCode(resultCode.getData());
        } else {
            return resultCode;
        }
        return R.ok(buildingService.save(building));
    }

    /**
     * 修改楼栋
     *
     * @param building 楼栋
     * @return R
     */
    @Operation(summary = "修改楼栋", description = "修改楼栋")
    @SysLog("修改楼栋")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('assets_building_edit')")
    public R updateById(@RequestBody BuildingEntity building) {
        return R.ok(buildingService.updateById(building));
    }

    /**
     * 通过id删除楼栋
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除楼栋", description = "通过id删除楼栋")
    @SysLog("通过id删除楼栋")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_building_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(buildingService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param building 查询条件
     * @param ids      导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_building_export')")
    public List<BuildingEntity> export(BuildingEntity building, Long[] ids) {
        return buildingService.list(Wrappers.lambdaQuery(building).in(ArrayUtil.isNotEmpty(ids), BuildingEntity::getId, ids));
    }

    /**
     * 楼栋全量查询
     *
     * @param building 楼栋管理
     * @return
     */
    @Operation(summary = "试验全量查询", description = "试验全量查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('assets_building_view')")
    public R getBuildingList(@ParameterObject BuildingDTO building) {
        LambdaQueryWrapper<BuildingEntity> wrapper = Wrappers.lambdaQuery();
        // 项目
        wrapper.in(ArrayUtil.isNotEmpty(building.getProjectId()), BuildingEntity::getProjectId, building.getProjectId());
        // 楼栋编号
        wrapper.like(StrUtil.isNotBlank(building.getCode()), BuildingEntity::getCode, building.getCode());
        // 楼栋名称
        wrapper.like(StrUtil.isNotBlank(building.getName()), BuildingEntity::getName, building.getName());
        // 建筑面积
        if (ArrayUtil.isNotEmpty(building.getFloorArea()))
            wrapper.ge(BuildingEntity::getFloorArea, building.getFloorArea()[0]).le(BuildingEntity::getFloorArea, building.getFloorArea()[1]);
        // 使用面积
        if (ArrayUtil.isNotEmpty(building.getUseArea()))
            wrapper.ge(BuildingEntity::getUseArea, building.getUseArea()[0]).le(BuildingEntity::getUseArea, building.getUseArea()[1]);
        return R.ok(buildingService.list(wrapper));
    }
}